/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* ���Ҫ�ڳ�����ʹ�ô˴���,���ڳ�����ע��ʹ����STC�����ϼ�����            */
/*---------------------------------------------------------------------*/

#include	"Task.h"
#include    "dc_motor.h"
#include    "STC8G_H_GPIO.h"
#include    "STC8G_PCA.h"


__idata int16 duty = 512;
__idata u16 pca_it16 = 0;
__xdata u16 ts_key1;
__xdata u16 ts_key2;

void ConfigKeys()
{
    P54 = 1;
    P55 = 1;
	P5_MODE_IO_PU(GPIO_Pin_4);  // SCL, Rx, Key1
	P5_MODE_IO_PU(GPIO_Pin_5);  // SDA, Tx, Key2, LED
    INT2_Enable();
    INT3_Enable();
}

void ConfigPCA()
{
    //PCA_USE_P34P35P36P37();
    PCA_INT_ENABLE();
}

u8 KeyUpdate(__xdata u16* ts)
{
    if(*ts - pca_it16 > 2){
        *ts = pca_it16;
        return TRUE;
    }else{
        return FALSE;
    }
}

void Key1(void) __interrupt INT2_VECTOR
{
    INT2_Clear();
    if(KeyUpdate(&ts_key1)){
        if(duty > 0){
            duty -= 64;
        }
        SetMotor1024(duty);
    }
}

void Key2(void) __interrupt INT3_VECTOR
{
    INT3_Clear();
    if(KeyUpdate(&ts_key2)){
        if(duty < 1024){
            duty += 64;
        }
        SetMotor1024(duty);
    }
}

void PCA_INT(void) __interrupt PCA_VECTOR
{
    if(CF){
        CF = 0;
        pca_it16++;
    }
}